USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_VALORES_SEGUROS]    Script Date: 11/24/2011 11:31:02 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('WASP_M0013_CABECERA_VALORES_SEGUROS') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0013_CABECERA_VALORES_SEGUROS]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_VALORES_SEGUROS]    Script Date: 11/24/2011 11:31:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[WASP_M0013_CABECERA_VALORES_SEGUROS](@FECHA_INICIO CHAR(8), @FECHA_TERMINO AS CHAR(8)) AS  
BEGIN

  SELECT  a.ppsuc
        , a.ppcta
        , a.ppoper
        , a.ppimp11
        , a.ppimp12
        , a.ppimp13
        , a.ppimp14
        , a.ppimp15
        , a.ppimp16
        , a.ppimp17
        , a.ppimp18
        , a.ppimp19
  into dbo.cabecera_valores_segurosIP_aux      
  FROM dbo.fsd611 AS a WITH (nolock) 
        CROSS JOIN  dbo.fsd601 AS b WITH (nolock) 
        CROSS JOIN  dbo.JT73109 AS d WITH (nolock)
  WHERE (a.pptipo = '') 
        AND (a.ppexte = 0) 
        AND (a.pgcod = b.pgcod) 
        AND (a.ppmod = b.ppmod) 
        AND (a.ppsuc = b.ppsuc) 
        AND (a.ppmda = b.ppmda) 
        AND (a.ppcta = b.ppcta) 
        AND (a.ppoper = b.ppoper) 
        AND (a.ppsbop = b.ppsbop) 
        AND (a.pptope = b.pptope) 
        AND (b.d601co = 'S') 
        AND (d.JT73109FPA = a.ppoper) 
        AND (d.JT73109SUC = a.ppsuc) 
        AND (d.JT73109FAP BETWEEN @FECHA_INICIO AND @FECHA_TERMINO)
  GROUP BY a.ppsuc, a.ppcta, a.ppoper, a.ppimp11, a.ppimp12, a.ppimp13, a.ppimp14, a.ppimp15, a.ppimp16, a.ppimp17, a.ppimp18, a.ppimp19
  
  
  select  a.ppsuc
        , a.ppcta
        , a.ppoper
        , a.ppimp11
        , a.ppimp12
        , a.ppimp13
        , a.ppimp14
        , a.ppimp15
        , a.ppimp16
        ,	a.ppimp17
        , a.ppimp18
        , a.ppimp19
  into dbo.cabecera_valores_segurosIP
  from dbo.cabecera_valores_segurosIP_aux as a
	CROSS JOIN fsd010 c (nolock)
  where (a.ppsuc = c.aosuc) 
        and	(a.ppoper = c.aooper) 
        and (a.ppcta = c.aocta) 
        and	(c.aosbop = 0)
END
  
GO


